package com.hhkj.admin.course.service.extend.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hhkj.admin.course.domain.CourseBankType;
import com.hhkj.admin.course.model.dopo.CourseBankTypeDo;
import com.hhkj.admin.course.model.dopo.CourseBankTypePo;
import com.hhkj.admin.course.service.base.CourseBankTypeService;
import com.hhkj.admin.course.service.extend.CourseBankTypeExtendService;
import com.ruoyi.common.base.biz.service.extend.BaseExtendServiceImpl;
import com.ruoyi.common.cont.SysConst;
import com.ruoyi.common.exception.GlobalException;
import org.springframework.stereotype.Service;

/**
 * CourseBankTypeServiceExtendImpl
 *
 * @author HHKJ-SERVER-ZJ
 * @since 2023/6/8 10:37
 */
@Service
public class CourseBankTypeExtendServiceImpl
        extends BaseExtendServiceImpl<CourseBankType, CourseBankTypeDo, CourseBankTypePo, CourseBankTypeService>
        implements CourseBankTypeExtendService {

    @Override
    public CourseBankType getDefaultTypeByCourseId(Long courseId) {
        LambdaQueryWrapper<CourseBankType> typeQuery = new LambdaQueryWrapper<>();
        typeQuery.eq(CourseBankType::getCourseId, courseId)
                .eq(CourseBankType::getDefaultFlag, SysConst.Flag.IS.getFlag());
        CourseBankType typePo = this.getService().getOne(typeQuery, false);
        if (ObjectUtil.isNull(typePo)) {
            throw new GlobalException("题库未配置，请联系管理员");
        }
        return typePo;
    }

    @Override
    public CourseBankType getByCourseIdAndIdNotDisable(Long courseId, Long id) {
        LambdaQueryWrapper<CourseBankType> typeQuery = new LambdaQueryWrapper<>();
        typeQuery.eq(CourseBankType::getCourseId, courseId)
                .eq(CourseBankType::getId, id);
        CourseBankType typePo = this.getService().getOne(typeQuery, false);
        if (ObjectUtil.isNull(typePo)) {
            throw new GlobalException("题库类型信息不存在");
        }
        if (SysConst.Flag.IS.getFlag().equals(typePo.getDisableFlag())) {
            throw new GlobalException("所选课程类型已禁用");
        }
        return typePo;
    }
}
